package my.leetcode.solution;

import java.util.Arrays;

/**
 * 思路:
 */
public class InterviewQuestion0104 {

    public static void main(String[] args) {
        System.out.println(canPermutePalindrome("tactcoa"));
        System.out.println(canPermutePalindrome("abc"));
        System.out.println(canPermutePalindrome("abb"));
    }

    public static boolean canPermutePalindrome(String s) {
        if(s.length() < 2) {
            return true;
        }
        char[] cs = s.toCharArray();
        Arrays.sort(cs);
        int l = cs.length - 1;
        if((cs.length & 1) == 0) {
            for(int i = 0;i < l;i+=2) {
                if(cs[i] == cs[i + 1]) {
                    continue;
                } else {
                    return false;
                }
            }
        } else {
            boolean flag = true;
            for(int i = 0;i < l;i+=2) {
                if(cs[i] != cs[i + 1]) {
                    if(flag) {
                        flag = false;
                        i--;
                        continue;
                    }
                    return false;
                } else {
                    continue;
                }
            }
        }
        return true;
    }
}
